package com.arrogames.blondbattle.game.combat 
{
	import ash.core.Engine;
	import ash.core.NodeList;
	import ash.tools.ListIteratingSystem;
	import com.arrogames.blondbattle.game.combat.Health;
	import com.arrogames.blondbattle.game.movement.Motion;
	import com.arrogames.blondbattle.game.movement.Position;
	import com.arrogames.blondbattle.game.EntityCreator;
	import com.arrogames.blondbattle.game.combat.MissileNode;
	import org.as3commons.logging.api.ILogger;
	import org.as3commons.logging.api.getLogger;
		
	/**
	 * ...
	 * @author ...
	 */
	public class MissileSystem extends ListIteratingSystem 
	{
		
		private static const logger:ILogger = getLogger(MissileSystem);
		private var entityCreator:EntityCreator = null;
		private var targets:NodeList;
	
		public function MissileSystem(pEntityCreator:EntityCreator)
		{
			entityCreator = pEntityCreator;
			super(MissileNode, updateNode);
		}
		
		private function updateNode(missileNode:MissileNode, time:Number) : void
		{
			//check if the missile hits its target, if so deal the damage and destroy the missile
			//if the target gets below 0 health it needs to be destroyed also
			var missilePosition:Position = missileNode.position;
			var damage:Number = missileNode.missile.damage;
			var range:Number = missileNode.range.value;
			var faction:Faction = missileNode.faction;
			//check if the missile extended its range.
			var distanceTraveled:Number = missilePosition.getDistanceTraveled();
			
			//check the direction of the missile
			var missileMotion:Motion = missileNode.motion;
			var scoredHit:Boolean = false;
			var targetNode:TargetNode = null;
			var targetPosition:Position;
			for (targetNode = targets.head; targetNode; targetNode = targetNode.next ) {
				if (targetNode.faction.name != faction.name) {
					targetPosition = targetNode.position;
					//if the missile is flying left, check if the position of the missile is smaller then that of the target
					if ( missileMotion.velocity.x < 0) {
						if ( missilePosition.position.x < targetPosition.position.x) {
							scoredHit = true;
							break;
						}
					//if the missile is flying right check if the position of the missile is bigger then that of the target
					} else {
						if ( missilePosition.position.x > targetPosition.position.x) {
							scoredHit = true;
							break;
						}
					}
				}
			}
		
			//hit score, substract health
			if (scoredHit) {
				targetNode.health.value -= damage;
				logger.debug("target " + targetNode.entity.name + " got hit for: " + damage + ", health: " + targetNode.health.value );
				entityCreator.destroyEntity(missileNode.entity);
				entityCreator.createHitIndator(missilePosition, "- " + damage);
				entityCreator.createExplosion(missilePosition);
			} else {
				if ( distanceTraveled > range ) {
					logger.debug("Missile traveled for: " + distanceTraveled + " and extended its range of: " + range);
					entityCreator.destroyEntity(missileNode.entity);
				} 
			}
			
		}
		
		override public function addToEngine(engine:Engine) : void
		{
			super.addToEngine(engine);
			targets = engine.getNodeList(TargetNode);
		}
		
	}

}